<?php
class Controller extends System{
    	
	var $name = 'pages';
	var $layout = 'default';
	var $page_title = null;
	var $bar_title = null;
    var $render = true;
    
	protected function view( $view, $vars = null ){   
		
		if(!in_array($this->name.'/'.$view, $this->permission)){
			die('Não tem permissão para acessar essa url.');
		}
		
		if( is_array($vars) && count($vars) > 0 )
        	extract ($vars, EXTR_PREFIX_SAME, 'view');
			
        if ( !file_exists($layout = VIEWS . 'layout/' . $this->layout . '.ctp') )
        	die("Houve um erro. Layout nao existe.");
            
		if ( !file_exists($file = VIEWS . $this->name. '/' . $view . '.ctp') )
			die("Houve um erro. View nao existe.");
		
		//Setando variaveis para todas as views
		$user = $this->USER;
		$session = $this->SESSION;
		$course = $this->course;
		$manager_users = $this->manager_users;
						
		$page_title = $this->page_title;
		$bar_title = $this->bar_title;			
		
		if($this->render){
			ob_start();
			include $file;
			$content_for_layout = ob_get_clean();
			
			require_once( $layout );
		}else{
			require_once( $file );
		}
        
    }
    
    protected function setMens($mens) {
    	$this->SESSION->mens = $mens;
    }
    
    protected function setErro($mens) {
    	$this->SESSION->erro = $mens;
    }
    
    function getErro() {

    	if($this->SESSION->erro){
    		$mens = $this->SESSION->erro;
    		$this->SESSION->erro = null;
    		echo '<div id="erroMens">'.$mens.'</div>';
    	}else{
    		return '';
    	}
    	
    }
    
    function getMens() {
    	 
    	if($this->SESSION->mens){
    		$mens = $this->SESSION->mens;
    		$this->SESSION->mens = null;
    		return '<div id="sucMens">'.$mens.'</div>';
    	}else{
    		return '';
    	}
    	 
    }
    
    /**
     * 
     * Enter description here ...
     * @param unknown_type $text
     */
    function retiraAcentos($text, $enc = 'UTF-8') {
    	
    	$acentos = array(
	        'A' => '/&Agrave;|&Aacute;|&Acirc;|&Atilde;|&Auml;|&Aring;/',
	        'a' => '/&agrave;|&aacute;|&acirc;|&atilde;|&auml;|&aring;/',
	        'C' => '/&Ccedil;/',
	        'c' => '/&ccedil;/',
	        'E' => '/&Egrave;|&Eacute;|&Ecirc;|&Euml;/',
	        'e' => '/&egrave;|&eacute;|&ecirc;|&euml;/',
	        'I' => '/&Igrave;|&Iacute;|&Icirc;|&Iuml;/',
	        'i' => '/&igrave;|&iacute;|&icirc;|&iuml;/',
	        'N' => '/&Ntilde;/',
	        'n' => '/&ntilde;/',
	        'O' => '/&Ograve;|&Oacute;|&Ocirc;|&Otilde;|&Ouml;/',
	        'o' => '/&ograve;|&oacute;|&ocirc;|&otilde;|&ouml;/',
	        'U' => '/&Ugrave;|&Uacute;|&Ucirc;|&Uuml;/',
	        'u' => '/&ugrave;|&uacute;|&ucirc;|&uuml;/',
	        'Y' => '/&Yacute;/',
	        'y' => '/&yacute;|&yuml;/',
	        'a.' => '/&ordf;/',
	        'o.' => '/&ordm;/'
		);
		
		$text = preg_replace($acentos, array_keys($acentos), htmlentities($text,ENT_NOQUOTES, $enc));
		$text = strtolower($text);
		
		return $text;
    	    	
    }
        
}